package com.ypx.ec.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ypx.ec.product.dao.AttrAttrgroupRelationDao;
import com.ypx.ec.product.dao.AttrDao;
import com.ypx.ec.product.entity.AttrAttrgroupRelationEntity;
import com.ypx.ec.product.entity.AttrEntity;
import com.ypx.ec.product.vo.AttrGroupVo;
import com.ypx.ec.product.vo.skuitemvo.SpuItemAttrGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ypx.ec.common.utils.PageUtils;
import com.ypx.ec.common.utils.Query;

import com.ypx.ec.product.dao.AttrGroupDao;
import com.ypx.ec.product.entity.AttrGroupEntity;
import com.ypx.ec.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrDao attrDao;
    @Autowired
    private AttrAttrgroupRelationDao relationDao;
    @Override // AttrGroupServiceImpl.java
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        // key不为空
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj) ->
                    obj.eq("attr_group_id", key).or().like("attr_group_name", key)
            );
        }
        if (catelogId == 0) {
            // Query可以把map封装为IPage
            IPage<AttrGroupEntity> page =
                    this.page(new Query<AttrGroupEntity>().getPage(params),
                            wrapper);
            return new PageUtils(page);
        } else {
            // 增加id信息
            wrapper.eq("catelog_id", catelogId);

            IPage<AttrGroupEntity> page =
                    this.page(new Query<AttrGroupEntity>().getPage(params),
                            wrapper);
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrGroupVo> queryAllGroupAndAttr(Long catelogId) {

        List<AttrGroupEntity> groups=baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id",catelogId));
        List<AttrGroupVo> ans=groups.stream().map((group)->{
            AttrGroupVo attrGroupVo=new AttrGroupVo();
            List<Long> addrIds=relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>()
                    .eq("attr_group_id",group.getAttrGroupId())).stream().map((ele)->{
                        return ele.getAttrId();
            }).collect(Collectors.toList());
            List<AttrEntity> attrs=attrDao.selectList(new QueryWrapper<AttrEntity>().in("attr_id",addrIds));
            attrGroupVo.setAttrs(attrs);
            return attrGroupVo;
        }).collect(Collectors.toList());

        return ans;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        //1.查出当前spu对应的所有属性分组信息以及当前分组下所有属性对应的值
        //1.1当前spu有多少属性分组
        AttrGroupDao baseMapper = this.getBaseMapper();
        List<SpuItemAttrGroupVo> vos = baseMapper.getAttrGroupWithAttrsBySpuId(spuId, catalogId);
        return vos;
    }
}